﻿package 
{
	import flash.events.Event;
	import flash.display.Sprite;

	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.controls.Label;

	import fl.data.DataProvider;
	import flash.external.ExternalInterface;

	public class Chart extends Sprite
	{
		public var dataPoints:Array;
		public var colorArray:Array;
		public var chartAttribute:String;
		protected var attributeNames:Array;
		protected var showControls:Boolean;
		protected var chartx:Number;
		protected var charty:Number;
		
		public function Chart()
		{
			colorArray = [
				0xCC3300, 0x006666, 0xFFCC00, 0x006633,
				0x99FF00, 0xFFFF00, 0xFF9900, 0xFF6600,
				0x333366, 0x0000FF, 0x009966, 0x339900,
				0xFF3300, 0xFF0000, 0x660066, 0x660099
			];
			dataPoints = new Array();
			attributeNames = new Array();
			showControls = false;
			ExternalInterface.addCallback("ParseData", ParseData);
			ExternalInterface.addCallback("DebugCallback", DebugCallback);
			ExternalInterface.addCallback("SetControls", SetControls);
		}
		
		protected function SetControls(newState:String) : void
		{
			showControls = (newState == 'On');
			DrawControls();
		}
		
		protected function DrawControls() : void
		{
			if(getChildByName('MainTitle') != null)
			{
				removeChild(getChildByName('MainTitle'));
			}
			
			if(getChildByName('MainLegend') != null)
			{
				removeChild(getChildByName('MainLegend'));
			}
			
			if(!showControls)
			{
				return void;
			}
			
			DrawTitle();
			//DrawLegend();
		}
		
		protected function DrawLegend() : void
		{
			var datagrid:DataGrid = new DataGrid();
			var dp:DataProvider = new DataProvider();
			for(var dataPoint in dataPoints)
			{
				dp.addItem({label:attributeNames[dataPoint], data:colorArray[dataPoint]});
			}
			
			var attributeCol:DataGridColumn = new DataGridColumn('label');
			attributeCol.headerText = 'Attribute';
			
			datagrid.addColumn(attributeCol);
			datagrid.setSize(150, 75);
			datagrid.name = 'MainLegend';
			datagrid.dataProvider = dp;
			datagrid.move(0, datagrid.height / 2);
			datagrid.setStyle('cellRenderer', LegendCellRenderer);
			addChild(datagrid);
		}

		protected function DrawTitle() : void
		{
			var currentAttribute:Label = new Label();
			currentAttribute.name = 'MainTitle';
			if(chartAttribute != null) {
				currentAttribute.text = chartAttribute;
			} else {
				currentAttribute.text = 'No Attribute Selected';
			}
			currentAttribute.autoSize = 'left';
			currentAttribute.move(0, 0);
			addChild(currentAttribute);
		}
		
		public function SortDataPoints(a:Array, b:Array) : int
		{
			var atotal:Number = 0;
			var btotal:Number = 0;
			
			for(var i:int = 0; i < a.length; i++)
			{
				atotal += a[i];
			}
			
			for(var j:int = 0; j < b.length; j++)
			{
				btotal += b[j];
			}
			
			atotal /= a.length;
			btotal /= b.length;

			
			if(btotal > atotal)
			{
				return 1;
			} else if( atotal > btotal) {
				return -1;
			}
			return 0;
		}

		protected function ScaleY(dataPoint:Number) : Number
		{
			return dataPoint;
		}
		
		public function DebugCallback(input:String) : void
		{
			ExternalInterface.call("DebugCallback", input);
		}

		public function ParseData(inputData:Object)
		{
			dataPoints = new Array();
		}
	}
}